package ly.gupao.edu.designPattern.prototype.Deep1Clonable;

import java.io.*;
import java.util.ArrayList;

public class ClonableTest {


    /**
     * 使用自己实现克隆方法原型模式创建对象
     *
     * @param args
     */
//    public static void main(String[] args) {
//        MailTemplate mailTemplate = new MailTemplate("title");
//        Mail mail = new Mail("name", "number", mailTemplate);
//
//
//        try {
//            Mail mailCopy = (Mail) mail.clone();
//            MailTemplate mailTemplateAnother = new MailTemplate("title Another");
//            mail.setMailTemplate(mailTemplateAnother);
//
////            System.out.println(mailCopy.getMailTemplate() == mail.getMailTemplate());
//            System.out.println(mail.getMailTemplate());
//            System.out.println(mailCopy.getMailTemplate());
//        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//        }
//
//    }

    /**
     * 创建一个类，实现clonable的接口，并且类中包含一个ArrayList的属性内容，并重写toString方法
     */
    static class Shallow implements Cloneable, Serializable{
        private ArrayList arrayList;

        /**
         * 构造函数通过传入一个集合 进行实例化
         * @param arrayList
         */
        Shallow(ArrayList arrayList){
            this.arrayList = arrayList;
        }

        /**
         * 直接重写clone方法
         * @return
         * @throws CloneNotSupportedException
         */
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Shallow shallow = (Shallow) super.clone();
            this.arrayList = (ArrayList) this.arrayList.clone();
            return shallow;
        }

        /**
         * 方便观察
         * @return
         */
        @Override
        public String toString() {
            return arrayList.toString();
        }
    }

    public static void main(String []args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        //为实例化一个Shallow对象创建一个集合，集合中添加一个字符串的对象
        ArrayList list = new ArrayList();
        list.add("a");
        //实例化SHallo对象
        Shallow shallow = new Shallow(list);
        //1、将s1对象序列化为一个数组
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(shallow);

        byte mailByte[] = byteArrayOutputStream.toByteArray();


        //2、将字节数组中的内容反序列化为一个Sheep对象
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mailByte);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        list.add("b");
        Shallow shallowCopy = (Shallow) objectInputStream.readObject();
        System.out.println(shallow.arrayList == shallowCopy.arrayList);


//        //拷贝出来一个SHallow对象
//        Shallow shallowCopy = (Shallow) shallow.clone();
//        //注意，这个 “b” 是在拷贝后添加的内容，如果说shallowCopy与shallow是两个独立的对象，那么会输出什么
//        list.add("b");
//        // 输出:[a,b]
//        System.out.println(shallow);
//        // 输出:[a,b]
//        System.out.println(shallowCopy);
//
//        System.out.println(shallow.arrayList == shallowCopy.arrayList);
//

    }

}
